<?php


    // Author: Tomasz Durka
    // Date: 5th February 2007
    // Prints out a link for PHP nails
    
    
  function button_link ($label, array $params = array()) {
  

    
    $module = mod_suffix ? '/' . mod_suffix : '';
    $controller = module;
    $action = action;
    $parameter = false;
    $style = ' style="text-decoration: none;"';
    $class = '';
    $on_click = '';
    $confirm = '';
    $id = '';
    
    if (!isset($params['action']) && module != $params['module']) {
      $action = false;
    }
    
    
    if (isset($params['active_if'])) {
      $active_if = $params['active_if'];
      if ($active_if)
        $class = ' class="active"';
    }
    
    foreach($params as $_key => $_val) {
      switch ($_key) {
        case 'parameter':
          $$_key = '/' . $_val;
          break;
        case 'confirm':
          $confirm = $_val;
          $confirm = preg_replace("/\n/", "\\\\n", $confirm);
          $confirm = str_replace("\"", "&quot;", $confirm);
          $confirm = "if (!confirm('{$confirm}')) return false;";
          break;
        case 'class':
          if ($active_if)
            $$_key = " {$_key}=\"{$_val}_active\"";
          else
            $$_key = " {$_key}=\"{$_val}\"";
          break;
        case 'style':
          $style = " style=\"{$_val}; text-decoration: none;\"";
          break;
        case 'id':
          $$_key = " {$_key}=\"{$_val}\"";
          break;
        case 'on_click':
          $$_key = htmlspecialchars($_val);
          $$_key = str_replace("\n", "\\n", $$_key);
          break;
        case 'url':
        case 'location':
          if ($_val ==  'back') {
            $location = $_SERVER['HTTP_REFERER'];
          } elseif (is_array($_val)) {
            $used_route = unserialize(USED_ROUTE);
            global $params;
            $params = array_merge($used_route[1], $_val);
            //pc($params);
            $route = '$route = "' . preg_replace('/\$([^\/]+)/', '{$params[\'\1\']}' , $used_route[0]) . '";';
            eval($route);
            $location = $host_suffix . '/s_' . $_val;
          } elseif ($params['url'] === false || $params['location'] === false) {
            $location = false;
          } else {
            $location = $host_suffix . '/' . $_val;
          }
          break;
        default:
          $$_key = $_val;
          break;
      }
    }
    
    
    
    if (isset($location)) {
      if ($location == false) {
        $location = '';
        $on_click .= "; return false;";
      }
      return "<a href=\"{$location}\" onClick=\"{$confirm}{$on_click}\"{$style}{$class}{$id}><input type=\"button\" value=\"{$label}\"/ ></a>";
    } else {
      $href = "{$module}/{$controller}/{$action}{$parameter}";
      return "<a href=\"{$href}\" onClick=\"{$confirm}{$on_click}\"{$style}{$class}{$id}><input type=\"button\" value=\"{$label}\"/ ></a>";
    }
  }

?>